import React, { PureComponent } from "react";
import { Icon } from "antd";
import Link from "umi/link";
import Debounce from "lodash-decorators/debounce";
import { formatMessage, FormattedMessage } from "umi-plugin-react/locale";
import styles from "./index.less";
import RightContent from "./RightContent";
import UserSession from "@/UserSession";
import ywzdlogo from "@/assets/ywzdlogo.png";

class GlobalHeader extends PureComponent {
	componentWillUnmount() {
		this.triggerResizeEvent.cancel();
	}
	/* eslint-disable*/
	@Debounce(600)
	triggerResizeEvent() {
		// eslint-disable-line
		const event = document.createEvent("HTMLEvents");
		event.initEvent("resize", true, false);
		window.dispatchEvent(event);
	}
	toggle = () => {
		const { collapsed, onCollapse } = this.props;
		onCollapse(!collapsed);
		this.triggerResizeEvent();
	};
	changeSystemMenu = (e) => {
		const { dispatch, location } = this.props;
		if (e == "artificialCenter") {
			return window.open(window.kefuUrl);
		};
		// alert(JSON.stringify(location))
		const activeMenu = UserSession.getActiveMenu();
		//缓存当前系统的菜单路由除机器人交互体验和首页
		if (activeMenu != 'interactCenter' && activeMenu != 'homeCenter') {
			if (UserSession.getLeftMenu()) {
				const leftMenuInfo = JSON.parse(UserSession.getLeftMenu());
				let search = location && location.search ? location.search.indexOf("?") === -1 ? "?" + location.search : location.search : ''
				leftMenuInfo[activeMenu] = location ? location.pathname + search : '/';
				window.localStorage.setItem("hideMenu", activeMenu);
				UserSession.setLeftMenu(JSON.stringify(leftMenuInfo))
			} else {
				let obj = {}
				let search = location && location.search ? location.search.indexOf("?") === -1 ? "?" + location.search : location.search : ''
				obj[activeMenu] = location ? location.pathname + search : '/';
				window.localStorage.setItem("hideMenu", activeMenu);
				UserSession.setLeftMenu(JSON.stringify(obj))
			}
		}

		dispatch({
			type: "user/changeSystemMenu",
			payload: e,
		});
	};

	toFirstMenu = (menuList) => {
		this.changeSystemMenu(menuList[0].key)
	}

	render() {
		const { collapsed, isMobile, logo, isHome } = this.props;
		const menus = UserSession.getSystemMenu();
		const activeMenu = UserSession.getActiveMenu();
		if (!menus) {
			return false;
		};
		const menuArr = [
			{ tenantCenter: formatMessage({ id: 'component.globalHeader.menu.tenantCenter' }) },
			{ interactCenter: formatMessage({ id: 'component.globalHeader.menu.interactCenter' }) },
      { workOrderCenter: formatMessage({ id: 'component.globalHeader.menu.workOrderCenter' }) },
      { knowledgeCenter: formatMessage({ id: 'component.globalHeader.menu.knowledgeCenter' }) },
      { llmCenter: formatMessage({ id: 'component.globalHeader.menu.llmCenter' }) },
      { statisticsCenter: formatMessage({ id: 'component.globalHeader.menu.statisticsCenter' }) },
			{ robotCenter: formatMessage({ id: 'component.globalHeader.menu.robotCenter' }) },
			{ staffCenter: formatMessage({ id: 'component.globalHeader.menu.staffCenter' }) },
			{ securityCenter: formatMessage({ id: 'component.globalHeader.menu.securityCenter' }) },
		];
		let menuList = menuArr.map((item) => {
			const menu = {};
			menu.menus = [];
			Object.keys(item).forEach((key) => {
				menu.name = item[key];
				menu.key = key;
				Object.keys(menus).forEach((m) => {
					if (m == key) {
						menu.menus = [...menus[m]];
					}
				});
			});
			return menu;
		}).filter((item) => item.menus.length > 0);

		const platformInfo = window.localStorage.getItem("platformInfo")

		return (
			<div className={styles.header}>
				{/* {isHome && ( */}
				<span className={styles.logo} key='logo' onClick={() => this.toFirstMenu(menuList)}>
					{/**<img src={platformInfo && JSON.parse(platformInfo).indexLogoUrl ? `${fileUrl + JSON.parse(platformInfo).indexLogoUrl}` : logo} alt='logo' style={{ width: 102, height: 36 }} /> */}
          <img src={ywzdlogo} alt='logo' style={{ width: 102, height: 36 }} />
				</span>
				{/* )} */}
				<span className={styles.nav}>
					{/**
						<span style={{ display: "inline-block", cursor: "pointer" }} className={activeMenu == "homePage" ? styles.active : ""} onClick={() => this.changeSystemMenu("homePage")}>
							<a>首页</a>
						</span>
        			*/}
					{/* <span style={{ display: "inline-block", cursor: "pointer" }} onClick={() => this.changeSystemMenu("getPage")}><a>机器人交互体验</a></span> */}
					{menuList && menuList.map((m) => {
						return (
							<span key={m.name} style={{ cursor: "pointer" }} className={activeMenu == m.key ? styles.active : ""} onClick={() => this.changeSystemMenu(m.key)}>
								<a>{m.name}</a>
							</span>
						);
					})}
				</span>
				<RightContent {...this.props} />
			</div>
		);
	}
}

export default GlobalHeader;